package com.gimi.cloud.bbp.manage.consumer.service.sys.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.base.BaseDTO;
import com.gimi.cloud.bbp.dto.log.LogOperationDTO;
import com.gimi.cloud.bbp.manage.consumer.feign.PublicFeignService;
import com.gimi.cloud.bbp.manage.consumer.service.sys.PublicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统通用接口实现类
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-26
 **/
@Service
public class PublicServiceImpl implements PublicService{
    private static final Logger logger = LoggerFactory.getLogger(PublicServiceImpl.class);

    private PublicFeignService publicFeignService;

    @Autowired
    public PublicServiceImpl(PublicFeignService publicFeignService) {
        this.publicFeignService = publicFeignService;
    }

    @Override
    public Response fileUpload(HttpServletRequest request, HttpServletResponse response, BaseDTO dto) throws Exception {
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        Response resp = new Response();
        if(null == multipartFiles || multipartFiles.size() == 0){
            resp.setResponseCode(ResponseCodeEnum.UNKNOWN_ERROR,"请选择文件");
            return resp;
        }
        String type = request.getParameter("type");
        if(StringUtil.isBlank(type)){
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"未知文件类型");
            return resp;
        }
        String extra = request.getParameter("extra");
        return publicFeignService.fileUpload(multipartFiles,type,extra);
    }

    @Override
    public Response validateCode(HttpServletRequest request, HttpServletResponse response, BaseDTO dto) throws Exception {
        return publicFeignService.getValidateCode(dto);
    }

    @Override
    public String getFilePath(HttpServletRequest request, HttpServletResponse response, String fileName){
        Response resp = publicFeignService.getFilePath(fileName);
        if(null != resp){
            return String.valueOf(resp.getRes_data());
        }
        //TODO 需返回默认的无图片地址
        return "";
    }

    @Override
    public Response sendOperationLogToMq(HttpServletRequest request, HttpServletResponse response, LogOperationDTO dto) throws Exception {
        return publicFeignService.sendOperationLogToMq(dto);
    }
}
